package main

import (
	"fmt"
	"net/http"
	"net/http/httputil"
)

/**
 * client
 */
func main() {
	// baseClient()
	mobileClient()
}

func mobileClient() {
	const url = "https://www.imooc.com/"
	request, _ := http.NewRequest(http.MethodGet, url, nil)
	request.Header.Add("User-Agent",
		"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 "+
			"(KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1")
	client := http.Client{
		CheckRedirect: func(
			req *http.Request, // 每次请求目标放在request
			via []*http.Request) error { // 存放所有重定向路径
			fmt.Println("Redirect:", req)
			return nil // 返回nil 让其重定向, 返回错误会终止重定向
		},
	}
	resp, err := client.Do(request)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	// 查看resp类型
	s, e := httputil.DumpResponse(resp, true)
	if e != nil {
		panic(e)
	}
	fmt.Printf("%s\n", s)
}

func baseClient() {
	resp, err := http.Get("https://coding.imooc.com")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	// 查看resp类型
	s, e := httputil.DumpResponse(resp, true)
	if e != nil {
		panic(e)
	}
	fmt.Printf("%s\n", s)
}
